﻿using System;
using System.ServiceModel;
using Remotion.Linq.Utilities;
using YunBox2.Contracts.Core;
using YunBox2.Server.Core;

namespace YunBox2.Server.Services
{
    public partial class BoxService
    {
        private SessionContext GetMySessionContext(Guid clientID)
        {
            if (clientID == Guid.Empty)
            {
                throw new ArgumentEmptyException("clientID");
            }

            SessionContext sessionContext;
            if (SessionList.CurrentSessionList.ContainsKey(clientID))
            {
                sessionContext = SessionList.CurrentSessionList[clientID];
            }
            else
            {
                sessionContext = new SessionContext {ID = clientID};
                SessionList.CurrentSessionList[clientID] = sessionContext;
            }
            return sessionContext;
        }


        private SessionContext GetMySessionContext()
        {
            Guid sessionId = GetMsgHeader().SessionId;

            Console.WriteLine("当前ClientId:{0}", sessionId.ToString("D"));

            SessionContext sessionContext;
            if (SessionList.CurrentSessionList.ContainsKey(sessionId))
            {
                sessionContext = SessionList.CurrentSessionList[sessionId];
            }
            else
            {
                sessionContext = new SessionContext {ID = sessionId};
                SessionList.CurrentSessionList[sessionId] = sessionContext;
            }
            return sessionContext;
        }

        private MsgHeader GetMsgHeader()
        {
            return OperationContext.Current.IncomingMessageHeaders.GetHeader<MsgHeader>(MsgHeader.HeaderLocalName, MsgHeader.HeaderNamespace);
        }
    }
}